#include "MainWindow.h"

#include "../dialogs/AddressDialog.h"
#include <QDebug>
#include <QInputDialog>
#include <QMessageBox>

MainWindow::MainWindow()
    : title_label(new QLabel("你画我猜", this))
    , start_button(new QPushButton("开始游戏", this))
    , widget_layout(new QVBoxLayout(this))
{
    setWindowTitle(title_label->text());
    setFixedSize(400, 300);

    title_label->setAlignment(Qt::AlignCenter);

    widget_layout->addStretch();
    widget_layout->addWidget(title_label);
    widget_layout->addStretch();
    widget_layout->addWidget(start_button);
    widget_layout->addStretch();

    connect(
        start_button, SIGNAL(clicked()), this, SLOT(onStartButtonClicked()));
}

void MainWindow::onStartButtonClicked()
{
    auto dialog = new AddressDialog(this);
    if (dialog->exec() == QDialog::Accepted) {
        // 获取玩家名称
        bool ok;
        auto name = QInputDialog::getText(this, "输入姓名", "输入姓名", QLineEdit::Normal, "", &ok);
        if (!ok)
            return;
        else if (name == "") {
            QMessageBox::critical(this, "错误", "姓名不能为空");
            return;
        }

        // 显示游戏窗口
        this->hide();
        gameWindow = new WaitingWindow(dialog->getAddress(), name);
        gameWindow->show();

        connect(gameWindow, &WaitingWindow::closed, this, [=]() {
            this->show();
            QMessageBox::information(this, "提示", "连接中止");
        });
    }
}
